package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.PriceReference;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.PriceReferenceService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/price/reference")
@Api(tags = "维修价格参照接口")
public class PriceReferenceController {


    @Autowired
    private PriceReferenceService priceReferenceService;

    @ApiOperation(value = "根据ID查询维修价格")
    @GetMapping("/{id}")
    public JsonResult<PriceReference> findById(@PathVariable Long id) {
        PriceReference priceReference = priceReferenceService.getById(id);
        return JsonResult.ok(priceReference);
    }

    @ApiOperation(value = "分页查询维修价格")
    @GetMapping
    public JsonResult<Page<PriceReference>> findById(Page<PriceReference> page, String name, Byte status) {
        page.addOrder(OrderItem.desc("created"));
        Page<PriceReference> priceReferencePage = priceReferenceService.page(page, new LambdaQueryWrapper<PriceReference>()
                .eq(status != null, PriceReference::getStatus, status)
                .eq(!StringUtils.isEmpty(name), PriceReference::getName, name));
        return JsonResult.ok(priceReferencePage);
    }


    @ApiOperation(value = "添加维修价格信息")
    @PostMapping
    public JsonResult add(@RequestBody PriceReference priceReference) {
        int count = priceReferenceService.count(new LambdaQueryWrapper<PriceReference>()
                .eq(PriceReference::getStatus, 1)
                .eq(PriceReference::getName, priceReference.getName()));
        if (count > 0) {
            throw new ApiException(priceReference.getName() + " 已经存在!");
        }
        Long userId = SecurityUtils.getUserId();
        priceReference.setCreateBy(userId);
        priceReference.setId(IdWorker.getId());
        priceReference.setCreated(new Date());
        boolean isOK = priceReferenceService.save(priceReference);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改维修价格信息")
    @PutMapping
    public JsonResult update(@RequestBody PriceReference priceReference) {
        int count = priceReferenceService.count(new LambdaQueryWrapper<PriceReference>()
                .eq(PriceReference::getStatus, 1)
                .eq(PriceReference::getName, priceReference.getName()));
        if (count > 1) {
            throw new ApiException(priceReference.getName() + " 已经存在!");
        }
        boolean isOK = priceReferenceService.updateById(priceReference);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除维修价格信息")
    @DeleteMapping("/{id}")
    public JsonResult del(@PathVariable Long id) {
        boolean isOK = priceReferenceService.removeById(id);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改维修价格信息状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody PriceReference priceReference) {
        boolean isOK = priceReferenceService.updateById(priceReference);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
